<?php

class Domain_Manager
{


    public function login($username, $password)
    {
        $rs = array();
        $model = new Model_Manager();


        $rs = $model->login($username, $password);
        return $rs;
    }

    public function callDoctor($doctor_id, $event_id)
    {//$doctor_id 是数组
        $doctor_android = array();
        $doctor_ios = array();
        $doctor_rp = array();

        $response_model = new Model_Response();
        $doctor_model = new Model_Doctor();
        $event_model=new Model_Event();

        $push = new Domain_PushToList();
        $redis = new Redis();
        $redis->connect('127.0.0.1', 6379,300);

        // $fp = $push->Connect();
        $event_model->update($event_id,array("Status"=>1));
        $event_tem=$event_model->get($event_id,array("Title","Illness_info","Location","CreateDate"));
        $content="0$".
            $event_tem["Location"]."$".
            $event_tem["Illness_info"]."$".
            $event_tem["CreateDate"];
        for ($i = 0; $i < count($doctor_id); $i++) {
            $Rpid = uniqid("Rp");
            $response_model->insert(array("id" => $Rpid, "Event_id" => $event_id, "Doctor_id" => $doctor_id[$i]));

            $doctor_rp[$i] = $Rpid;

            $rsd = $doctor_model->get($doctor_id[$i], array("SSID","Device"));
            //print_r($rsd);
            if( $rsd["Device"]==0){$doctor_ios[] = $rsd["SSID"];}
            if( $rsd["Device"]==1) {$doctor_android[] = $rsd["SSID"];}
        }

        $push_info=json_encode(array('content'=>$content,'doctor_android'=> $doctor_android,'doctor_ios'=>$doctor_ios));
        $redis->RPUSH('tess', $push_info);
        //DI()->redis->set_lPush('tess',$push_info);

        return $doctor_rp;

    }

    public function dispatchDoctors($doctor_id, $event_id)
    {//$doctor_id 是数组
        if($doctor_id==null||count($doctor_id)==0){return "no doctor";}
        $doctor_android = array();
        $doctor_ios = array();
        $doctor_rp = array();

        $response_model = new Model_Dispatch();
        $doctor_model = new Model_Doctor();
        $event_model=new Model_Event();
        $event_tem=$event_model->get($event_id,array("Title","Illness_info","Location","CreateDate"));
        $content="0$".
            $event_tem["Location"]."$".
            $event_tem["Illness_info"]."$".
            $event_tem["CreateDate"];
        $redis = new Redis();
        $redis->connect('127.0.0.1', 6379,300);
        // $push = new Domain_Push();
        //$fp = $push->Connect();

        for ($i = 0; $i < count($doctor_id); $i++) {
            $Rpid = uniqid("Di");

            $pushFail = array();

            $response_model->insert(array("id" => $Rpid, "Event_id" => $event_id, "Doctor_id" => $doctor_id[$i]));
            $event_model->update($event_id,array("Status"=>2));
            $doctor_rp[$i] = $Rpid;


            $rsd = $doctor_model->get($doctor_id[$i], "SSID","Device");

            if( $rsd["Device"]==0){$doctor_ios[] = $rsd["SSID"];}
            if( $rsd["Device"]==1) {$doctor_android[] = $rsd["SSID"];}

        }
        // $push->closeConnect();
        $push_info=json_encode(array('content'=>$content,'doctor_android'=> $doctor_android,'doctor_ios'=>$doctor_ios));

        $redis->RPUSH('tess', $push_info);
        return $doctor_rp;

    }

    public function getResponse($response_id)
    {
        $model = new Model_Response();
        $rs = $model->getResponse($response_id);
        return $rs;
    }
}